package com.bdqn.t350.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class UploadAliYunUtil {
    //阿里云域名
    public static final String ALI_DOMAIN = "https://yuanxiaoxiang.oss-cn-guangzhou.aliyuncs.com/";
    //地域节点
    static final String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
    //Bucket名称
    static final String bucket = "yuanxiaoxiang";


    //阿里云AccessKey管理生成的信息,在阿里云账号里面查找该信息
    static final String accessKeyId = "LTAI5tMPqqWZLASGU6oW5NTC";
    static final String accesskeySecret = "kVgqaaT6McV2ztTRnvzLstofoCtf2a";

    /**
     * @param titleImg     页面传递给后端的附件对象
     * @param saveFileName 上传到oss生成的文件名
     * @return
     */
    public static boolean uploadImage(MultipartFile titleImg, String saveFileName) {
        boolean bool = false;
        //oss客户端对象-类似你要连接mysql是不是需要去通过账号密码地址等信息去连接数据库
        //通过账号,key等信息去连接阿里云存储
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accesskeySecret);
        try {
            //具体将文件写入到那个地方
            ossClient.putObject(bucket, saveFileName, titleImg.getInputStream());
            bool = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            ossClient.shutdown();
        }
        return bool;
    }
}
